package com.api.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.JSONPResponseBodyAdvice;
import com.api.MEnumError;
import com.api.MErrorEnum;
import com.api.view.chemicalInfo.*;
import com.base.api.ApiException;
import com.base.api.annotation.ApiMethod;
import com.base.http.util.HttpClientUtil;
import com.base.util.StringUtil;
import com.hyd.dao.model.AgeParam;
import com.hyd.dao.model.AgeParamExample;
import com.hyd.dao.model.ChemicalType;
import com.hyd.dao.model.ChemicalTypeExample;
import com.hyd.dao.model.*;
import com.hyd.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;
import sun.swing.StringUIClientPropertyKey;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/chemical", produces = { "application/json;charset=UTF-8" })
@Api(tags = "元素详情")
public class ApiInfoController {
    @Autowired
    private ChemicalTypeService chemicalTypeService;
    @Autowired
    private AgeParamService ageParamService;
    @Autowired
    private XAxisService xAxisService;
    @Autowired
    private EvidenceService evidenceService;


    @ApiOperation(value = "图片返回结果", notes = "不需要登录")
    @RequestMapping(value = "/picReturnMsg", method = RequestMethod.POST)
    @ApiMethod
    public OrcReturnView picReturnMsg(@ApiParam(value = "id", required = true) String id){
        String result = HttpClientUtil.createPost("http://58.87.92.158:7878/dataSysServer/RecogRequest").form("PicAddr","http://62.234.104.49:8080/download?id="+id).send();

        //新建化验单实例
        Evidence evidence = new Evidence();
        evidence.setImg(id);

        JSONObject json = JSONObject.parseObject(result).getJSONObject("data");
        OrcReturnView orcReturnView = JSONObject.toJavaObject(json,OrcReturnView.class);

        JSONArray head = JSONObject.parseObject(result).getJSONArray("header") ;
        orcReturnView.setFormHeadJson(head.toJSONString());
        evidence.setHeaderInfo(head.toString());
        evidence.setLogId(orcReturnView.getLog_id() == null? "":orcReturnView.getLog_id());
        evidence.setName(orcReturnView.getName() == null? "":orcReturnView.getName());
        evidence.setAge(orcReturnView.getAge() == null?"":orcReturnView.getAge());
        evidence.setSex(orcReturnView.getSex()==null?"":orcReturnView.getSex());


        JSONArray end = JSONObject.parseObject(result).getJSONArray("tail") ;
        orcReturnView.setFormEndJson(end.toJSONString());
        evidence.setHeadTail(end.toString());

        List<OrcListView> chemicalList = new ArrayList<>();
        JSONArray jsonArray = json.getJSONArray("info");
        List<OrcReturnModel> modelList = jsonArray.toJavaList(OrcReturnModel.class);
        if(modelList.size() == 0){
            throw new ApiException(MEnumError.CONTENT_NOEXIST_ERROR);
        }
        evidence.setDataInfo(jsonArray.toString());
        evidenceService.insert(evidence);

        ChemicalTypeExample example = new ChemicalTypeExample();
        List<ChemicalType> list = chemicalTypeService.selectByExample(example);

        for(ChemicalType ex : list){
            OrcListView c = new OrcListView();
            for(int i = 0; i<modelList.size(); i++){
                if(ex.getName().equals(modelList.get(i).getChemical())){
                    //元素名相同
                    c.setChemical(ex.getName());
                    c.setId(ex.getId());
                    if(modelList.get(i).getRange().contains("-")){
                        int index = modelList.get(i).getRange().indexOf("-");
                        String min = modelList.get(i).getRange().substring(0,index);
                        String max = modelList.get(i).getRange().substring(index+1,modelList.get(i).getRange().length());
                        c.setMinRange(min);
                        c.setMaxRange(max);
                    }else if(modelList.get(i).getRange().contains("阴") || modelList.get(i).getRange().contains("阳")){
                        c.setRange(modelList.get(i).getRange());
                    }

                    c.setRangeInfo(modelList.get(i).getRangeInfo());
                    c.setChemicalName(ex.getChemicalName());
                    c.setType(ex.getType());
                    c.setResult(modelList.get(i).getResult());
                    chemicalList.add(c);
                }else{
                    if(StringUtil.isNotBlank(ex.getDicInfo())){
                        if(ex.getDicInfo().contains(modelList.get(i).getChemical())){
                            //元素名相同
                            c.setChemical(ex.getName());
                            c.setId(ex.getId());
                            if(modelList.get(i).getRange().contains("-")){
                                int index = modelList.get(i).getRange().indexOf("-");
                                String min = modelList.get(i).getRange().substring(0,index);
                                String max = modelList.get(i).getRange().substring(index+1,modelList.get(i).getRange().length());
                                c.setMinRange(min);
                                c.setMaxRange(max);
                            }else if(modelList.get(i).getRange().contains("阴") || modelList.get(i).getRange().contains("阳")){
                                c.setRange(modelList.get(i).getRange());
                            }

                            c.setRangeInfo(modelList.get(i).getRangeInfo());
                            c.setChemicalName(ex.getChemicalName());
                            c.setType(ex.getType());
                            c.setResult(modelList.get(i).getResult());
                            chemicalList.add(c);
                        }
                    }
                }

            }
        }
        if(chemicalList.size() == 0){
            throw new ApiException(MEnumError.CONTENT_NOEXIST_ERROR);
        }
        orcReturnView.setList(chemicalList);
        return orcReturnView;
    }

    @ApiOperation(value = "根据元素修改识别内容", notes = "不需要登录")
    @RequestMapping(value = "/updateChemicalInfo", method = RequestMethod.POST)
    @ApiMethod
    public ChemicalInfoView updateChemicalInfo(@ApiParam(value = "元素名称", required = true) String name){
        ChemicalTypeExample example = new ChemicalTypeExample();
        example.createCriteria().andNameEqualTo(name);
        List<ChemicalType> list = chemicalTypeService.selectByExample(example);
        if(list.size() != 1){
            throw new ApiException(MEnumError.ERROR_CHEMICAL_NAME);
        }
        ChemicalInfoView c = new ChemicalInfoView();
        c.setId(list.get(0).getId());
        c.setChemicalInfoName(list.get(0).getChemicalName());
        return c;
    }


    @ApiOperation(value = "获取详情", notes = "不需要登录")
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    @ApiMethod
    public ChemicalInfoView getInfo( Integer id){
        ChemicalInfoView c = new ChemicalInfoView();
        //查询元素类型表
        ChemicalType chemicalType = chemicalTypeService.selectByPrimaryKey(id);
        if(null == chemicalType){
            throw new ApiException(MEnumError.CONTENT_NOEXIST_ERROR);
        }
        c.setName(chemicalType.getName());
        c.setChemicalName(chemicalType.getChemicalName());
        c.setType(chemicalType.getType());
        c.setInfoBegin(chemicalType.getInfoBegin()==null?"" : chemicalType.getInfoBegin());
        c.setInfo(chemicalType.getInfo());
        c.setRangeMin(chemicalType.getRangeMin());
        c.setRangeMax(chemicalType.getRangeMax());
        c.setResult(chemicalType.getRangUpInfo());
        //单位
        c.setRangeInfo(chemicalType.getRangeInfo() == null? "" : chemicalType.getRangeInfo());
        //查询参数
        AgeParamExample example = new AgeParamExample();
        example.createCriteria().andChemialTypeEqualTo(chemicalType.getId());
        List<AgeParam> ageParamList = ageParamService.selectByExampleWithBLOBs(example);
        c.setParamJson(ageParamList.get(0).getData());
        //查询横坐标
//        XAxisExample axisExample = new XAxisExample();
//        axisExample.createCriteria().andChemicalTypeIdEqualTo(chemicalType.getId());
//        List<XAxis> xAxisList = xAxisService.selectByExample(axisExample);
//        c.setxParamRange(xAxisList.get(0).getAgeRange());
//        c.setxParamSpace(xAxisList.get(0).getAgeSpace());
        //查询纵坐标
//        YAxisExample yAxisExample = new YAxisExample();
//        yAxisExample.createCriteria().andChemicalTypeIdEqualTo(chemicalType.getId());
//        List<YAxis> yAxisList = yAxisService.selectByExample(yAxisExample);
//        c.setyParamRange(yAxisList.get(0).getParamRange());
        return c;
    }
}
